package main

import (
	"base_go/base/structbean"
	"fmt"
)

// 面向对象的使用

// 定义结构体
type Teacher struct {
	 Name string
	 Age int
	 School string
}
type Student struct {
	Age int
}
type Stu Student // 定义Stu是Student类型
// 面向对象
func main() {
  //useStruct()
  //transformStruct()
  //invokeTeacher()
  useOtherPackage()
}


// 使用其它包的struct
func useOtherPackage(){
	c := structbean.Computer{
		Typename: "lenove",
		Price:    100.0,
		Desc:     "30",
	}
	fmt.Println(c)
}

// 调用给Teacher定义的方法
func invokeTeacher(){
	t:=Teacher{}
	fmt.Println(t)
	t = t.testTeacher("hello",23,"上海大学")
	fmt.Println(t)
	fmt.Println(&t) // 会按照String的方法输出
}
// 给Teacher Struct绑定方法
func (t Teacher) testTeacher(name string,age int,shool  string)(Teacher){
	return Teacher{name,age,shool}
}
// 实现string方法 类似于toString 输出的时候就会按照该方式
func (s *Teacher) String() string{
	str := fmt.Sprintf("Name = %v , Age = %v",s.Name,s.Age)
	return str
}
// 结构体的转换
func transformStruct(){
	var s1 Student = Student{Age:10}
	var s2 Stu = Stu{Age:20}
	s2 = Stu(s1)  // Stu是Student的子类

	fmt.Println(s2)
}

// 结构体的初始化
func useStruct(){
	var t1 Teacher
	t1.Name="马老师"
	t1.Age=20
	t1.School="清华大学"
	fmt.Println(t1)
    // 直接赋值使用
	var t2 Teacher = Teacher{"张力",30,"北京大学"}
	fmt.Println(t2)
    // 指针赋值
    var t3 *Teacher = new(Teacher)
	(*t3).Name="刘飞"
	(*t3).Age = 80
	t3.School = "浙江大学" // go语言底层做了优化
	fmt.Println(*t3)

	// 指针赋值
	var t4 *Teacher = &Teacher{"高就",90,"北京"}
	fmt.Println(*t4)
}

